package it.francescogabbrielli.android.solidalapp;

import java.util.zip.DeflaterInputStream;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.util.Log;

/** TODO: provide inputstream to a zipped string */
public class LogProvider extends ContentProvider {
	
    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
	private static final String AUTH = LogProvider.class.getPackage().getName()+".logs";
    {
	    URI_MATCHER.addURI(AUTH, "*", 1);
    }

	@Override
	public boolean onCreate() {
		return true;
	}

	@Override
	public String getType(Uri uri) {
		
		String name = uri.getPathSegments().get(0);
		
		switch (URI_MATCHER.match(uri)) {
		
		case 1:
			return "vnd.android.cursor.item/vnd." + AUTH + "." + name;
			
		default:
			break;
		}
		
		return null;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		
		MatrixCursor cursor = null;		
		
		switch (URI_MATCHER.match(uri)) {
		case 1:
			try {
				Process process = Runtime.getRuntime().exec("logcat -d");
//				BufferedReader bufferedReader = new BufferedReader(
//						new InputStreamReader(process.getInputStream()));
//				StringBuilder logs = new StringBuilder();
//				String line = "";
//				while ((line = bufferedReader.readLine()) != null)
//					logs.append(line);
				
				new DeflaterInputStream(process.getInputStream());
				
				
			} catch (Exception e) {
				Log.e(SolidalApp.TAG, "Cannot report issue", e);
			}
			break;
		}
		
		return cursor;
		
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		return 0;
	}
}
